import unittest
import tkinter as tk
from main import CourseManagement, Student, Class

class TestCourseManagementGUI(unittest.TestCase):

    def setUp(self):
        # 创建一个测试用的 tkinter 根窗口，但不会显示
        self.root = tk.Tk()
        self.root.withdraw()  # 隐藏窗口

    def tearDown(self):
        # 清理测试环境
        self.root.destroy()

    def test_gui_initialization(self):
        app = CourseManagement(self.root)
        self.assertIsNotNone(app)  # 确保 CourseManagement 实例化成功

    def test_gui_add_student(self):
        app = CourseManagement(self.root)
        # 模拟用户输入
        def mock_askstring(title, prompt):
            return "Test Student"
        tk.simpledialog.askstring = mock_askstring
        app.add_student_to_class()
        self.assertIn("Test Student", [s.name for s in app.classes[0].students])

    def test_gui_add_course(self):
        app = CourseManagement(self.root)
        # 模拟用户输入
        def mock_askstring(title, prompt):
            return "Test Course"
        tk.simpledialog.askstring = mock_askstring
        app.add_course()
        self.assertIn("Test Course", [c.name for c in app.courses])

if __name__ == "__main__":
    unittest.main()